var express = require('express');
var router = express.Router();
const jwt = require('jsonwebtoken');

const { wxLogin, auth, getUserInfo } = require('../controllers/users.js')

/* GET users listing. */
router.post('/wxLogin', async function(req, res, next) {
    const data = await wxLogin(req.body);
    res.send(data);
});

// 更新数据库
router.post('/auth', async function(req, res, next) {
    const token = req.get('token');

    const { _id, openid, session_key } = jwt.verify(token, 'PRIVATE_KEY')
    const data = await auth({...req.body, _id, openid, session_key });
    res.send(data);
});

// 获取用户数据在数据库中
router.get('/getUserInfo', async function(req, res, next) {
    const token = req.get('token');
    console.log(token, '获取假的用户信息');
    const { _id, openid, session_key } = jwt.verify(token, 'PRIVATE_KEY')
    const data = await getUserInfo({ _id });
    res.send(data);
});

module.exports = router;